package cn.zhz.controller;

import cn.zhz.pojo.games;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping
public class gamecontroller {
    private Logger logger = Logger.getLogger(gamecontroller.class);

    @Resource
    private gameService gameService;

    @RequestMapping(value = "/gamelist.html",method = RequestMethod.POST)
    public String getgamelist(Model model, @RequestParam(value = "gameName",required = false)String gameName,
                              @RequestParam(value = "gameType",required = false)String gameType,
                              @RequestParam(value = "gameCompany",required = false)String gameCompany,
                              @RequestParam(value = "gameYear",required = false)String gameYear){
        int num=0;
        if(gameYear!=null&&gameYear!=""){
            num = Integer.parseInt(gameYear);
        }
        List<games> gamesList = gameService.getlistgame(gameName,gameType,gameCompany,num);
        model.addAttribute("gamelist",gamesList);
        return "gamelist";
    }

    @RequestMapping(value = "/gameall.html",method = RequestMethod.GET)
    public String getgameall(Model model,@RequestParam(value = "error",required = false)String error){
        List<games> gamesList = gameService.getlistgame(null,null,null,0);
        model.addAttribute("gamelist",gamesList);
        model.addAttribute("error","添加成功！");
        return "gamelist";
    }

    @RequestMapping(value = "/add.html", method = RequestMethod.GET)
    public String add(@ModelAttribute("games")games games){
        return "gameadd";
    }

    @RequestMapping(value = "/add.html", method = RequestMethod.POST)
    public String addSave(games games,Model model){
        int num = gameService.addgameinfo(games);
        if(num>0){
            model.addAttribute("error","添加成功！");
            return "redirect:/gameall.html";
        }else{
            model.addAttribute("error","添加失败！");
            return "redirect:/add.html";
        }
    }
}
